package com.shop.user.controller;//package com.shop.user.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.shop.user.pojo.User;
import com.shop.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页查询
     * @param model
     * @param realName
     * @param roleName
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/usersel1")
    public String getSeletePage(Model model, String realName, String roleName, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        PageInfo<User> page = userService.selectByFengYe(realName, roleName, pageNum);
        model.addAttribute("userList",page);
        model.addAttribute("realName",realName);
        model.addAttribute("roleName",roleName);
        return "userlist";
    }


    @RequestMapping("/chaxun")
    public String chaxun(Model model,String account,String realName,Integer sex,String birthday,String phone,String address,String roleName){
        model.addAttribute("account",account);
        model.addAttribute("realName",realName);
        model.addAttribute("sex",sex);
        model.addAttribute("birthday",birthday);
        model.addAttribute("phone",phone);
        model.addAttribute("address",address);
        model.addAttribute("roleName",roleName);
        return "userview";
    }

    /**
     * 添加
     * @param model
     * @param user
     * @return
     */
    @RequestMapping("/adduser")
    public String useradd(Model model,String account, User user){
        int count = userService.count(account);
        if (count>0){
            userService.add(user);
            model.addAttribute("userList",userService.selectByFengYe("","",null));
        }else {
            return "useradd";
        }
        return "userlist";
    }
    @RequestMapping("/adduser1")
    public String add(){
        return "useradd";
    }

    /**
     * 删除
     * @param model
     * @param idss
     * @return
     */
    @RequestMapping("/deluser")
    @ResponseBody
    public String userdel(Model model,Integer idss){
        boolean del = userService.del(idss);
        model.addAttribute("userList",userService.selectByFengYe("","",null));
        return JSON.toJSONString(del);
    }

    /**
     * 更新
     * @param model
     * @param user
     * @return
     */
    @RequestMapping("/upduser1")
    public String userupd(Model model,User user){
        userService.upd(user);
        model.addAttribute("userList",userService.selectByFengYe("","",null));
        return "userlist";
    }
    @RequestMapping("/upduser2")
    public String upd(Model model,String account){
        model.addAttribute("account",account);
        return "usermodify";
    }
}
